<template>
	<view class="u-tabs" :style="{
		background: bgColor
	}">
		<!-- $u.getRect()对组件根节点无效，因为写了.in(this)，故这里获取内层接点尺寸 -->
		<view :id="id">
			<scroll-view scroll-x class="u-scroll-view" :scroll-left="scrollLeft" scroll-with-animation>
				<view class="u-scroll-box" :class="{'u-tabs-scorll-flex': !isScroll}">
					<view class="u-tab-item u-line-1" :id="'u-tab-item-' + index" v-for="(item, index) in list" :key="index" @tap="clickTab(index)"
					 :style="[tabItemStyle(index)]">
						<u-badge :count="item[count] || item['count'] || 0" :offset="offset" size="mini"></u-badge>
						{{ item[name] || item['name']}}
					</view>
					<view v-if="showBar" class="u-tab-bar" :style="[tabBarStyle]"></view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	
	export default {
		name: "u-tabs",
		props: {
			
			isScroll: {
				type: Boolean,
				default: true
			},
			
			list: {
				type: Array,
				default () {
					return [];
				}
			},
			
			current: {
				type: [Number, String],
				default: 0
			},
			
			height: {
				type: [String, Number],
				default: 80
			},
			
			fontSize: {
				type: [String, Number],
				default: 30
			},
			
			duration: {
				type: [String, Number],
				default: 0.5
			},
			
			activeColor: {
				type: String,
				default: '#2979ff'
			},
			
			inactiveColor: {
				type: String,
				default: '#303133'
			},
			
			barWidth: {
				type: [String, Number],
				default: 40
			},
			
			barHeight: {
				type: [String, Number],
				default: 6
			},
			
			gutter: {
				type: [String, Number],
				default: 30
			},
			
			bgColor: {
				type: String,
				default: '#ffffff'
			},
			
			name: {
				type: String,
				default: 'name'
			},
			
			count: {
				type: String,
				default: 'count'
			},
			
			offset: {
				type: Array,
				default: () => {
					return [5, 20]
				}
			},
			
			bold: {
				type: Boolean,
				default: true
			},
			
			activeItemStyle: {
				type: Object,
				default() {
					return {}
				}
			},
			
			showBar: {
				type: Boolean,
				default: true
			},
			
			barStyle: {
				type: Object,
				default() {
					return {}
				}
			},
			
			itemWidth: {
				type: [Number, String],
				default: 'auto'
			}
		},
		data() {
			return {
				scrollLeft: 0, 
				tabQueryInfo: [], 
				componentWidth: 0, 
				scrollBarLeft: 0, 
				parentLeft: 0, 
				id: this.$u.guid(), 
				currentIndex: this.current,
				barFirstTimeMove: true, 
			};
		},
		watch: {
			
			
			list(n, o) {
				
				if(n.length !== o.length) this.currentIndex = 0;
				
				this.$nextTick(() => {
					this.init();
				});
			},
			current: {
				immediate: true,
				handler(nVal, oVal) {
					
					this.$nextTick(() => {
						this.currentIndex = nVal;
						this.scrollByIndex();
					});
				}
			},
		},
		computed: {
			
			tabBarStyle() {
				let style = {
					width: this.barWidth + 'rpx',
					transform: `translate(${this.scrollBarLeft}px, -100%)`,
					
					'transition-duration': `${this.barFirstTimeMove ? 0 : this.duration }s`,
					'background-color': this.activeColor,
					height: this.barHeight + 'rpx',
					opacity: this.barFirstTimeMove ? 0 : 1,
					
					'border-radius': `${this.barHeight / 2}px`
				};
				Object.assign(style, this.barStyle);
				return style;
			},
			
			tabItemStyle() {
				return (index) => {
					let style = {
						height: this.height + 'rpx',
						'line-height': this.height + 'rpx',
						'font-size': this.fontSize + 'rpx',
						'transition-duration': `${this.duration}s`,
						padding: this.isScroll ? `0 ${this.gutter}rpx` : '',
						flex: this.isScroll ? 'auto' : '1',
						width: this.$u.addUnit(this.itemWidth)
					};
					
					if (index == this.currentIndex && this.bold) style.fontWeight = 'bold';
					if (index == this.currentIndex) {
						style.color = this.activeColor;
						
						style = Object.assign(style, this.activeItemStyle);
					} else {
						style.color = this.inactiveColor;
					}
					return style;
				}
			}
		},
		methods: {
			
			async init() {
				
				let tabRect = await this.$uGetRect('#' + this.id);
				
				this.parentLeft = tabRect.left;
				
				this.componentWidth = tabRect.width;
				this.getTabRect();
			},
			
			clickTab(index) {
				
				if(index == this.currentIndex){
					return this.$emit('again', index);
				}
				
				this.$emit('change', index);
			},
			
			getTabRect() {
				
				let query = uni.createSelectorQuery().in(this);
				
				for (let i = 0; i < this.list.length; i++) {
					
					query.select(`#u-tab-item-${i}`).fields({
						size: true,
						rect: true
					});
				}
				
				query.exec(
					function(res) {
						this.tabQueryInfo = res;
						
						this.scrollByIndex();
					}.bind(this)
				);
			},
			
			scrollByIndex() {
				
				let tabInfo = this.tabQueryInfo[this.currentIndex];
				if (!tabInfo) return;
				
				let tabWidth = tabInfo.width;
				
				let offsetLeft = tabInfo.left - this.parentLeft;
				
				let scrollLeft = offsetLeft - (this.componentWidth - tabWidth) / 2;
				this.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft;
				
				let left = tabInfo.left + tabInfo.width / 2 - this.parentLeft;
				
				this.scrollBarLeft = left - uni.upx2px(this.barWidth) / 2;
				
				
				if(this.barFirstTimeMove == true) {
					setTimeout(() => {
						this.barFirstTimeMove = false;
					}, 100)
				}
			}
		},
		mounted() {
			this.init();
		}
	};
</script>

<style lang="scss" scoped>
	@import "../../libs/css/style.components.scss";

	view,
	scroll-view {
		box-sizing: border-box;
	}

	/* #ifndef APP-NVUE */
	::-webkit-scrollbar,
	::-webkit-scrollbar,
	::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}
	/* #endif */

	.u-scroll-box {
		position: relative;
		/* #ifdef MP-TOUTIAO */
		white-space: nowrap;
		/* #endif */
	}

	/* #ifdef H5 */
	
	scroll-view ::v-deep ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}
	/* #endif */

	.u-scroll-view {
		width: 100%;
		white-space: nowrap;
		position: relative;
	}

	.u-tab-item {
		position: relative;
		/* #ifndef APP-NVUE */
		display: inline-block;
		/* #endif */
		text-align: center;
		transition-property: background-color, color;
	}

	.u-tab-bar {
		position: absolute;
		bottom: 0;
	}

	.u-tabs-scorll-flex {
		@include vue-flex;
		justify-content: space-between;
	}
</style>
